<script type="text/javascript" id="TMMscript">
	
/*
(The MIT License)

Copyright (c) 2008-2009 John Beales

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
	
	
	var TMM = {
		
		/* CONFIG:  add the Google Map key, URL of the KML file, map width, and map height here:  */
		gmap_key:'insert your google maps API here',
		kml_file:'insert your My Maps KML URL here',
//		The line above should look something like this, (with no slashes at the beginning):
//		kml_file:'http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&output=nl&msid=107444494116951053799.00043e81c596a6346fbb5',
				  
		map_width:500,
		map_height:400,
		initial_zoom:null, // this is optional.  If it is left as null zoom will default to the KML file's viewport
		/* That's it!  */

		map:null,
		kml:null,
		container:null,
		alrdyLoaded:false,
		markers:null,
		loadchecker:false,
		list:null,
		loadstatus:0,
		catchHandle:null,
		
		setup:function() {
			if(!document.getElementById('TMMscript')) { return false; }
			TMM.markers = [];
			var h = document.getElementsByTagName('head')[0];
			var s = document.createElement('script');
			s.setAttribute('type','text/javascript');
			s.id = 'TMM_gscript';
			h.insertBefore(s,h.firstChild);
			s.setAttribute('src','http://www.google.com/jsapi?async=2&callback=TMM.go&key='+TMM.gmap_key);


			var sc = document.getElementById('TMMscript');
			var sp = sc.parentNode;
			if(!document.getElementById('TMM_map')) {
				var m = document.createElement('div');
				m.setAttribute('id','TMM_map');
			} else {
				m = document.getElementById('TMM_map');
			}	
			m.style.width = TMM.map_width+'px';
			m.style.height = TMM.map_height+'px';
			
			
			if(!document.getElementById('TMM_container')) {
				TMM.container = document.createElement('div');
				TMM.container.setAttribute('id','TMM_container');
			} else {
				TMM.container = document.getElementById('TMM_container');
			}
			TMM.container.style.width = TMM.map_width+'px';
			
			TMM.container.appendChild(m);
			
			TMM.lst = document.createElement('ul');
			TMM.lst.setAttribute('id','TMM_selectlist');
			TMM.lst.style.height = Math.ceil(TMM.map_height/3)+'px';
			TMM.lst.style.overflow = 'auto';
			TMM.container.appendChild(TMM.lst);
			
			sp.insertBefore(TMM.container,sc.nextSibling);	
		},
		
		go:function() {
			if(typeof google == 'undefined') {
				TMM.loadchecker = setTimeout(TMM.go,10);
			} else {
				TMM.loadchecker = false;
		/*		google.load("maps", "2.132e", {"callback":TMM.mapsLoaded});  */
				google.load("maps", "2.150", {"callback":TMM.mapsLoaded});
			}
		
		},
		
		
		mapsLoaded:function() {
		  if (google.maps.BrowserIsCompatible() && document.getElementById("TMM_map")) {
			TMM.map = new google.maps.Map2(document.getElementById("TMM_map"));
			TMM.map.addControl(new google.maps.LargeMapControl());
			TMM.map.addControl(new google.maps.MapTypeControl());
			TMM.map.enableContinuousZoom();
			TMM.kml = new google.maps.GeoXml(TMM.kml_file);
			google.maps.Event.addListener(TMM.kml,'load',TMM.kmlLoaded);
			TMM.catchHandle = google.maps.Event.addListener(TMM.map,'addoverlay',TMM.catchOverlay);
		  }
		},
	
		catchOverlay:function(overlay) {
			if(overlay.name && !overlay.Known) {
				// Save a reference
				TMM.markers.push(overlay);
				// remember that we know about this one
				overlay.Known = true;
			}
		},
		
		
		kmlLoaded:function() {

			if(TMM.loadstatus == 0) {
				if(TMM.kml.hasLoaded()) {
					if(!TMM.alrdyLoaded) {
						TMM.kml.gotoDefaultViewport(TMM.map);
						TMM.map.addOverlay(TMM.kml);
						TMM.alrdyLoaded = true;
					}
				}
			} else if(TMM.loadstatus == 1) {
				google.maps.Event.removeListener(TMM.catchHandle);
				TMM.addList();
				if(TMM.initial_zoom !== null) {
					TMM.map.setZoom(TMM.initial_zoom);
				}
			}
			TMM.loadstatus++;
		},
		
		alphabetize:function(a,b) {
			if(a.name < b.name) {
				return -1;
			} else if (a.name > b.name) {
				return 1;
			} else {
				return 0;
			}
		
		},
		
		addList:function() {
		
			TMM.markers.sort(TMM.alphabetize);
			
			var a,li;
			for(var i=0;i<TMM.markers.length;i++) {
				a = document.createElement('a');
				a.setAttribute('style','cursor:pointer;');
				a.appendChild(document.createTextNode(TMM.markers[i].name));
				a.onclick = TMM.show;
				a.style.lineHeight = '40px';
				li = document.createElement('li');
				if('undefined' !== typeof TMM.markers[i].da) {
					li.style.background = "url("+TMM.markers[i].da.icon.image+") no-repeat left center";
				}
				li.style.paddingLeft = '32px';
				li.style.margin = '0.5em 0';
				li.style.listStyle = 'none';
				
				li.appendChild(a);
				TMM.lst.appendChild(li);
			}
		},
		
		show:function() {
			var idx = false;
			var as = TMM.lst.getElementsByTagName('a');
			for(var i=0;i<as.length;i++) {
				if(as[i] == this) {
					break;
				}
			}

			TMM.map.setZoom(15);
			if('undefined' !== typeof TMM.markers[i].ca.latlng) {
				TMM.map.panTo(new google.maps.LatLng(TMM.markers[i].ca.latlng.lat,TMM.markers[i].ca.latlng.lng)); // Thx. to Andrew Leach
			} else if('undefined' !== typeof TMM.markers[i].c.GP.x && 'undefined' !== typeof TMM.markers[i].c.GP.y) {
				TMM.map.panTo(new google.maps.LatLng(TMM.markers[i].c.GP.x,TMM.markers[i].c.GP.y));
			} 
			google.maps.Event.trigger(TMM.markers[i],'click');
		},
		
		addLoadEvent:function(func) {
			var oldonload = window.onload;
			if(typeof window.onload != 'function') {
				window.onload = func;
			} else {
				window.onload = function() {
					oldonload();
					func();
				};
			}
		}
	};
	
	TMM.addLoadEvent(TMM.setup);
</script>